﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmLogin : FormBase
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                txtUsername.Text = "admin";
                txtPassword.Text = "phung";
                //btnLogin_Click(null, null);

                txtUsername.Focus();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckInput())
                {
                    TaiKhoan obj = Session.DefaultSession.Query<TaiKhoan>().FirstOrDefault(c => c.TenTaiKhoan == txtUsername.Text);
                    if (obj != null)
                    {
                        if (obj.MatKhau == txtPassword.Text)
                        {
                            GlobalValues.UserID = obj.Oid;
                            GlobalValues.UserName = obj.TenTaiKhoan;
                            GlobalValues.IsAdmin = obj.QuanTri;
                            XPQuery<PhanQuyen> queryPhanQuyen = Session.DefaultSession.Query<PhanQuyen>();
                            List<int> lst = new List<int>();
                            foreach (var item in obj.PhanQuyens)
                                lst.Add(item.Khoa.Oid);
                            GlobalValues.Departments = lst.ToArray();
                            this.DialogResult = DialogResult.OK;
                        }
                        else
                            CommonMessage("Nhập sai mật khẩu");
                    }
                    else
                        CommonMessage("Tài khoản không tồn tại");
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private bool CheckInput()
        {
            bool result = true;
            dxErrorProvider1.ClearErrors();
            if (txtUsername.Text == string.Empty)
            {
                dxErrorProvider1.SetError(txtUsername, "Chưa nhập");
                result = false;
            }
            if (txtPassword.Text == string.Empty)
            {
                dxErrorProvider1.SetError(txtPassword, "Chưa nhập");
                result = false;
            }
            return result;
        }
    }
}
